package com.droidcon.tweetyourroute;

import android.os.Bundle;
import android.widget.TabHost;

import com.actionbarsherlock.app.SherlockFragmentActivity;

public class MainActivity extends SherlockFragmentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		
		TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();
		
		TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
                
                FavsFragment favsFragment = (FavsFragment) fm.findFragmentByTag("favs");
                CreateRouteFragment createRouteFragment = (CreateRouteFragment) fm.findFragmentByTag("create");
                SearchRouteFragment searchRouteFragment = (SearchRouteFragment) fm.findFragmentByTag("search");
                
                android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

                if (favsFragment != null) ft.detach(favsFragment);
                if (createRouteFragment != null) ft.detach(createRouteFragment);
                if (searchRouteFragment != null) ft.detach(searchRouteFragment);

                if (tabId.equalsIgnoreCase("favs")) {

                    if (favsFragment == null) {
                        ft.add(R.id.realtabcontent, new FavsFragment(), "favs");
                    } else {
                        ft.attach(favsFragment);
                    }

                } else if (tabId.equalsIgnoreCase("create")) {

                    if (createRouteFragment == null) {
                        ft.add(R.id.realtabcontent, new CreateRouteFragment(), "create");
                    } else {
                        ft.attach(createRouteFragment);
                    }

                } else {
                    if (searchRouteFragment == null) {
                        ft.add(R.id.realtabcontent, new SearchRouteFragment(), "search");
                    } else {
                        ft.attach(searchRouteFragment);
                    }
                }
                ft.commit();
            }
        };
	
        tabHost.setOnTabChangedListener(tabChangeListener);
        
        TabHost.TabSpec tSpecFavs = tabHost.newTabSpec("favs");
		tSpecFavs.setIndicator("Favs");
		tSpecFavs.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecFavs);

        TabHost.TabSpec tSpecNew = tabHost.newTabSpec("create");
        tSpecNew.setIndicator("Create");
        tSpecNew.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecNew);
        
        TabHost.TabSpec tSpecSearch = tabHost.newTabSpec("search");
        tSpecSearch.setIndicator("Search");
        tSpecSearch.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(tSpecSearch);
	}
}
